package com.yt.aop.cglib.demo;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;


public class UserBeanFactory {
	
	public static UserService getInstanse(){
		
		UserService us = new UserServiceImpl();
		MyAspectJ myAspectJ = new MyAspectJ();
		
		Enhancer eh = new Enhancer();
		//eh.setSuperclass(UserService.class);
		eh.setSuperclass(us.getClass());
		
		eh.setCallback(new MethodInterceptor() {
			
			@Override
			public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
				
				myAspectJ.before();
				Object obj = method.invoke(us, args);
				methodProxy.invokeSuper(proxy, args);
				myAspectJ.after();
				
				
				return obj;
			}
		});
		
		UserService usEh = (UserService) eh.create();
		return usEh;
	}
}
